package com.icloud.cer.rest.msg.bean;

import java.io.IOException;

import org.apache.log4j.Logger;

import com.icloud.cer.rest.Constants;
import com.icloud.cer.rest.msg.AbsMsgObject;
import com.icloud.cer.rest.msg.BDataFilterInputStream;
import com.icloud.cer.rest.msg.DataFilterOutputStream;
import com.icloud.cer.rest.util.StringUtils;

/**
 * 
 * 客户端(加密机)返回加密结果
 * 
 * @author hok
 *
 */
public class EncryptResponse extends AbsMsgObject<EncryptResponse>{
	private Logger log = Logger.getLogger(EncryptResponse.class);
	public static final int BYTE_COUNT = 372;
	private byte[] certification;//256
	private long sequence;//8
	private long taskId;//8
	private byte[] userId;//32
	private int errorCode;//4
	private byte[] errorMsg;//64

	// 'b' or 'l' b是高字节 l是低字节
	private char flag_;
	private String userIdStr;
	private String certificationStr;
	private String errorMsgStr;

	@Override
	protected void doSerialize(DataFilterOutputStream dos) throws IOException {
		if(StringUtils.isEmpty(certificationStr)) {
			dos.writeBytes(new String(new byte[256],Constants.CHARSET));
		} else {
			if(certificationStr.length() > 256) {
				log.fatal("cer lenght > 256.");
				certificationStr = certificationStr.substring(0, 256);
			}
			dos.writeBytes(new String(certificationStr.getBytes(),Constants.CHARSET));
			int cerRemainLen = 256 - certificationStr.length();
			if(cerRemainLen > 0) {
				dos.writeBytes(new String(new byte[cerRemainLen],Constants.CHARSET));
			}
		}
		dos.writeLong(sequence);//8
		dos.writeLong(taskId);//8
		dos.writeBytes(userIdStr);//32
		dos.writeInt(errorCode);//4
		
		
		
		if(StringUtils.isEmpty(errorMsgStr)) {
			dos.writeBytes(new String(new byte[64],Constants.CHARSET));
			return;
		} 
		dos.writeBytes(new String(errorMsgStr.getBytes(),Constants.CHARSET));
		int remainderLen = 64 - errorMsgStr.length();
		if(remainderLen > 0) {
			dos.writeBytes(new String(new byte[remainderLen],Constants.CHARSET));
		}
	}

	@Override
	protected EncryptResponse doDeserialize(BDataFilterInputStream dis) throws IOException {
		certification = new byte[256];
		userId = new byte[32];
		errorMsg = new byte[64];

		//按顺序进行反序列化
		dis.read(certification);//256
		sequence = dis.readLong();//8
		taskId = dis.readLong();//8
		dis.read(userId);//32
		errorCode = dis.readInt();//4
		dis.read(errorMsg);//64
		
		this.userIdStr = new String(userId, Constants.CHARSET).trim();
		this.certificationStr = new String(certification, Constants.CHARSET).trim();
		this.errorMsgStr = new String(errorMsg, Constants.CHARSET).trim();
		return this;
	}
	public byte[] getCertification() {
		return certification;
	}

	public void setCertification(byte[] certification) {
		this.certification = certification;
	}

	public long getSequence() {
		return sequence;
	}

	public void setSequence(long sequence) {
		this.sequence = sequence;
	}

	public long getTaskId() {
		return taskId;
	}

	public void setTaskId(long taskId) {
		this.taskId = taskId;
	}

	public byte[] getUserId() {
		return userId;
	}

	public void setUserId(byte[] userId) {
		this.userId = userId;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}

	public byte[] getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(byte[] errorMsg) {
		this.errorMsg = errorMsg;
	}

	public char getFlag_() {
		return flag_;
	}

	public void setFlag_(char flag_) {
		this.flag_ = flag_;
	}

	public String getUserIdStr() {
		return userIdStr;
	}

	public void setUserIdStr(String userIdStr) {
		this.userIdStr = userIdStr;
	}

	public String getCertificationStr() {
		return certificationStr;
	}

	public void setCertificationStr(String certificationStr) {
		this.certificationStr = certificationStr;
	}

	public String getErrorMsgStr() {
		return errorMsgStr;
	}

	public void setErrorMsgStr(String errorMsgStr) {
		this.errorMsgStr = errorMsgStr;
	}


}
